import routes from '@/router/mainMaps.js'

const setTabs = (path) => {
    localStorage.setItem('tabsActive', path)
    const foundRoute = findRouteByPath(routes, path);
    let list = []
    if (JSON.parse(localStorage.getItem('tabsList'))) {
        list = JSON.parse(localStorage.getItem('tabsList'))
    } else {
        list = [findRouteByPath(routes, '/')]
    }
    list.push(foundRoute)
    list = [...new Map(list.map(item => [item.path, item])).values()]
    localStorage.setItem('tabsList', JSON.stringify(list))
}

const findRouteByPath = (routes, targetPath) => {
    for (const route of routes) {
        if (route.path === targetPath) {
            return route;
        }
        if (route.children) {
            const found = findRouteByPath(route.children, targetPath);
            if (found) {
                return found;
            }
        }
    }
    return null;
}


export {setTabs}